<?php
namespace app\index\controller;
use think\Db;
class Index extends Base{
    public function index(){
        $param = input('get.');
        $address='';
        if ($param['type']==1) {
            $data=Db::name('street')->where('id',$param['id'])->find();
            $address=$data['city'].$data['street'];
        }elseif ($param['type']==2) {
            $data=Db::name('block')->where('id',$param['id'])->find();
            if ($data['plate']) {
            	$address=$data['city'].$data['street'].$data['plate'];
            }else{
            	$address=$data['city'].$data['street'].$data['estate'].$data['block'];
            }
        }elseif ($param['type']==3) {
            $data=Db::name('unit')->where('id',$param['id'])->find();
            $address=$data['city'].$data['street'].$data['plate'].$data['build'].$data['unit'];
        }elseif ($param['type']==4) {
            $data=Db::name('room')->where('id',$param['id'])->find();
            $address=$data['city'].$data['street'].$data['plate'].$data['estate'].$data['build'].$data['unit'].$data['room'];
        }
        $this->assign('address',$address);
        return $this->fetch();
    }
    public function phpexcel(){
        if(request()->isGet()){
         return $this->fetch();
        }
        if(request() -> isPost())
        {
            $file = request()->file('excel');
         //将文件保存到public/uploads目录下面
            $info = $file->validate(['size'=>1048576,'ext'=>'xls,xlsx'])->move( './uploads');
            if($info){
                //获取上传到后台的文件名
                $fileName = $info->getSaveName();
                //获取文件路径
                $filePath = "..".DIRECTORY_SEPARATOR.'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$fileName;
                //获取文件后缀
                $suffix = $info->getExtension();
                //判断哪种类型
                if($suffix=="xlsx"){
                    $reader = \PHPExcel_IOFactory::createReader('Excel2007');
                }else{
                    $reader = \PHPExcel_IOFactory::createReader('Excel5');
                }
            }else{
                $this->error('文件过大或格式不正确导致上传失败-_-!');
            }
            //载入excel文件
            $excel = $reader->load("$filePath",$encode = 'utf-8');
            //读取第一张表
            $sheet = $excel->getSheet(0);
            //获取总行数
            $row_num = $sheet->getHighestRow();
            //获取总列数
            $col_num = $sheet->getHighestColumn();
            $data = []; //数组形式获取表格数据
            for ($i = 1; $i <= $row_num; $i ++) {
                $data[$i]['uuid']  = $sheet->getCell("A".$i)->getValue();
                $data[$i]['city']  = $sheet->getCell("B".$i)->getValue();
                $data[$i]['street']  = $sheet->getCell("C".$i)->getValue();
                //将数据保存到数据库
            }
            Db::name('street')->insertAll($data);
        }
        return $this->fetch();
    }
}